package com.neusoft.tyut.hrs.comon;

import com.neusoft.tyut.hrs.comon.entity.Emp;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * TODO: 在任何一种情况下，都必须返回200 ，但是具体的内容由CommonResult 的 code属性决定
 *
 * @param <T>
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
    private Integer code = 200;
    private String msg = "success";
    private T result;

    public CommonResult(T result) {
        this.code = 200;
        this.msg = "success";
        this.result = result;
    }

    public CommonResult(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.result = null;
    }

    public static <T> CommonResult<T> success(T result) {
        return new CommonResult(result);
    }

    public static CommonResult success(Emp result) {
        return new CommonResult(result);
    }

    public static CommonResult fail(ResultCode resultCode) {
        return new CommonResult(resultCode);
    }

    public static CommonResult fail(int code, String msg) {
        return new CommonResult(code, msg, null);
    }
}
